﻿using ApisixSDK.Common.Excetpion;
using SufeiUtil;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ApisixSDK.Common.Http
{
    public class Connection
    {
        private HttpHelper client;
        private HttpItem httpItem;
        public Connection(int? connTimeout, int? readTimeout, int? writeTimeout)
        {
            client = new HttpHelper();
            httpItem = new HttpItem();
            this.httpItem.Timeout = connTimeout.Value;
            //TODO 判断哪个大赋值哪个
            this.httpItem.ReadWriteTimeout = writeTimeout.Value;

        }

        public virtual HttpResult DoRequest(HttpItem request)
        {
            HttpResult response = null;
            try
            {
                request.Timeout = this.httpItem.Timeout;
                request.ReadWriteTimeout = this.httpItem.ReadWriteTimeout;

                response = client.GetHtml(request);
            }
            catch (Exception e)
            {
                throw new ApisixSDKExcetion(e.GetType().FullName + " - " + e.Message);
            }
            return response;
        }

        public virtual HttpResult GetRequest(string url, WebHeaderCollection headers)
        {
            HttpResult response = null;
            try
            {
                HttpItem request = new HttpItem();
                request.URL = url;
                request.Timeout = this.httpItem.Timeout;
                request.ReadWriteTimeout = this.httpItem.ReadWriteTimeout;
                request.Header = headers;
                response = client.GetHtml(request);
            }
            catch (Exception e)
            {
                throw new ApisixSDKExcetion(e.GetType().FullName + " - " + e.Message);
            }
            return response;
        }

        public virtual HttpResult PutRequest(string url,string body, WebHeaderCollection headers)
        {
            HttpResult response = null;
            try
            {
                HttpItem request = new HttpItem();
                request.URL = url;
                request.Method = "PUT";
                request.Postdata = body;
                request.ContentType = headers.GetValues("Content-Type")[0];
                request.Timeout = this.httpItem.Timeout;
                request.ReadWriteTimeout = this.httpItem.ReadWriteTimeout;
                request.Header = headers;
                response = client.GetHtml(request);
            }
            catch (Exception e)
            {
                throw new ApisixSDKExcetion(e.GetType().FullName + " - " + e.Message);
            }
            return response;
        }
        public virtual HttpResult PostRequest(string url, string body, WebHeaderCollection headers)
        {
            HttpResult response = null;
            try
            {
                HttpItem request = new HttpItem();
                request.URL = url;
                request.Method = "POST";
                request.Postdata = body;
                request.ContentType = headers.GetValues("Content-Type")[0];
                request.Timeout = this.httpItem.Timeout;
                request.ReadWriteTimeout = this.httpItem.ReadWriteTimeout;
                request.Header = headers;
                response = client.GetHtml(request);
            }
            catch (Exception e)
            {
                throw new ApisixSDKExcetion(e.GetType().FullName + " - " + e.Message);
            }
            return response;
        }

        public virtual HttpResult DeleteRequest(string url, WebHeaderCollection headers)
        {
            HttpResult response = null;
            try
            {
                HttpItem request = new HttpItem();
                request.URL = url;
                request.Method = "DELETE";
                request.Timeout = this.httpItem.Timeout;
                request.ReadWriteTimeout = this.httpItem.ReadWriteTimeout;
                request.Header = headers;
                response = client.GetHtml(request);
            }
            catch (Exception e)
            {
                throw new ApisixSDKExcetion(e.GetType().FullName + " - " + e.Message);
            }
            return response;
        }
    }
}
